package roborally;

import Exceptions.ElementPlacementError;
import Exceptions.IllegalCoordinateException;
import be.kuleuven.cs.som.annotate.Basic;


/**
 * A class representing baterries with a weight and an energy amount to fill a robot's energy level.
 * @author Jef Keijers/Bachelor of Science: Informatics
 * @version 1.0
 */
public class Battery extends Item {
	
	/** 
	 * @param 	initialEnergy
	 * @param 	weight
	 * @pre 	
	 * 			| canHaveAsEnergy(initialEnergy)
	 * @effect	
	 * 			|this.setEnergy(new Energy(initialEnergy))
	 */	
	public Battery(double initialEnergy, int weight) {
		super(weight);
		assert (canHaveAsEnergy(initialEnergy));
		this.setEnergy(new Energy(initialEnergy));
	}	
	
	@Basic
	public int getMaxEnergy() {
		return MAX_ENERGY;
	}
	
	/**
	 * @param energy
	 * @return 
	 * 		| if ((energy <= 5000)&& (energy >=0)) then result == true
	 * 		| if ((energy > 5000)||(energy < 0)) the result == false
	 */
	public boolean canHaveAsEnergy(double energy){
		if ((getMaxEnergy()>=energy) && (energy>=0)) 
			return true;
		return false;		
	}
	
	private final static int MAX_ENERGY = 5000;
	
	/**
	 * @post | new.getEnergy().getValue() == this.getEnergy().getValue()+getEnergyToAddWhenShot()
	 */
	@Override
	public void handleShot() {
		this.getEnergy().setValue(this.getEnergy().getValue()+getEnergyToAddWhenShot()); 
	}

	/**
	 * @throws 	ElementPlacementError 
	 * @throws 	IllegalCoordinateException 
	 * @pre 	
	 * 			| (robot.getEnergy().getValue()+this.getEnergy().getValue())<=robot.getMaxEnergy()
	 * @effect	
	 * 			|robot.getEnergy().setValue(this.getEnergy().getValue());
	 * @effect	
	 * 			|this.terminate()
	 */
	@Override
	public void handleUse(Robot robot)  { 
		assert ((robot.getEnergy().getValue()+this.getEnergy().getValue())<=robot.getMaxEnergy());
		if (robot.terminateFromPossessions(this)) {
			robot.getEnergy().setValue(this.getEnergy().getValue());
			this.terminate();
		}
	}	
}
